package dal;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import entities.Movie;
import entities.Theater;

public class ManageTheater
{
	private SessionFactory			factory;
	private static ManageTheater	instance;

	@SuppressWarnings("deprecation")
	private ManageTheater()
	{
		try
		{
			factory = new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex)
		{
			System.err.println("Failed to create sessionFactory object." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static ManageTheater getInstance()
	{
		if (instance == null)
		{
			instance = new ManageTheater();
		}

		return instance;
	}

	public ArrayList<Integer> add(ArrayList<Theater> theatersToAdd)
	{
		ArrayList<Integer> IDsAdded = new ArrayList<Integer>();

		for (Theater theaterToAdd : theatersToAdd)
		{
			IDsAdded.add(this.add(theaterToAdd));
		}

		return IDsAdded;
	}

	public Integer add(Theater theaterToAdd)
	{
		Session session = factory.openSession();
		Transaction tx = null;
		Integer theaterID = null;

		try
		{
			tx = session.beginTransaction();
			theaterID = (Integer) session.save(theaterToAdd);
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
		return theaterID;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Theater> getAll()
	{
		ArrayList<Theater> theaters = null;
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			theaters = (ArrayList<Theater>) session.createQuery(
					"FROM entities.Theater").list();
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
		return theaters;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Theater> getBy(Movie byMovie)
	{
		ArrayList<Theater> theaters = null;
		Session session = factory.openSession();
		Transaction tx = null;
		Query q = null;

		try
		{
			tx = session.beginTransaction();
			q = session
					.createQuery("SELECT t"
							+ " FROM entities.Movie m,entities.Show s,entities.Theater t WHERE"
							+ " t.id = s.showTheater and m.id = s.showMovie and m.id = :m");
			q.setParameter("m", byMovie.getID());

			theaters = (ArrayList<Theater>) q.list();
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
		return theaters;
	}

	public void update(Integer theaterID, String name)
	{
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			Theater theater = (Theater) session.get(Theater.class, theaterID);
			theater.setTheaterName(name);
			session.update(theater);
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
	}

	public void delete(Integer TheaterID)
	{
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			Theater Theater = (Theater) session.get(Theater.class, TheaterID);
			session.delete(Theater);
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
	}

}
